<%-- 
    Document   : insertdelete
    Created on : 2024-6-1, 16:59:53
    Author     : Administrator
--%>

<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <%
              response.setCharacterEncoding("utf-8");
           request.setCharacterEncoding("utf-8");

            String sno=request.getParameter("sno");
            String sname=request.getParameter("sname");
            String sex=request.getParameter("sex");
            String dno=request.getParameter("dno")==null?"0":request.getParameter("dno");
            
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");//反射机制 加载注册驱动
           String url="jdbc:sqlserver://127.0.0.1:1433;databasename=dbtest";//localhost
              Connection con=DriverManager.getConnection(url, "sa", "123123");
//创建与数据库连接的对象，指明具体连接的数据库，特别注意：数据库必须是存在的
              Statement stmt=con.createStatement();//创建执行SQL语句的对象Statement
              //String student="insert into student(sno,sname,sex,dno) values('05','中夏','女','001')";//空可以不插，非空一定插
               //String student="update student set sno='"+sno+"',sname='"+sname+"',sex='"+sex+"',dno='"+dno+"' where sno='"+sno+"'";
//修改数据             
               String student="delete from student where sno='"+sno+"'";
//标识字段的不用插入，空的字段可以不插入，非空的一定插入数据
//插入数据时，主键，唯一列都不插入重复的值
//删除数据
              int num=stmt.executeUpdate(student);
              stmt.close();
              con.close();
             if(num>0){
                  request.setAttribute("message","删除成功");
                   request.getRequestDispatcher("selectfront.jsp").forward(request,response);
             }else{
                  request.setAttribute("message","删除失败");
                   request.getRequestDispatcher("selectfront.jsp").forward(request,response);
                 
             }

        
        
        %>
    </body>
</html>
